import {
	Message,
	Notification,
	Loading
} from 'element-ui';

const clientUuid = require('uuid');
let moment = require('moment');
let localforage = require('localforage')
import env from '@/common/utils/env.js'

let messageInstance = null;
let notifyInstance = null;
let loadingInstance = null;

/**
 * @function 消息提示
 * @param {String} text 提示内容
 * @param {String} type 'success' | 'warning' | 'info' | 'error'
 */
export function message(text, type) {
	loading(true);
	if (messageInstance != null) {
		Message.closeAll();
	}
	messageInstance = Message({
		message: text,
		type: type || "success",
		onClose() {
			messageInstance = null
		}
	})
}

/**
 * @function 消息通知
 * @param {String} text 消息内容
 * @param {String} type 'success' | 'warning' | 'info' | 'error'
 */
export function notify(text, type) {
	loading(true);
	if (notifyInstance != null) {
		Notification.closeAll();
	}
	notifyInstance = Notification({
		title: '消息通知',
		message: text,
		type: type || "success",
		onClose() {
			notifyInstance = null
		}
	})
}

/* 获取UUID */
export function uuid() {
	return clientUuid.v4();
}

/*  */
export function createClientId() {
	localforage.getItem(env.gmwframeworkId).then(data => {
		if (!data) {
			data = clientUuid.v4();
			localforage.setItem(env.gmwframeworkId, data);
		}
	});
}

/* loading */
export function loading(close) {
	loadingService({
		close: close,
		text: "加载中..."
	})
}

export function loadingService(options) {
	options = options || {};
	if (options.close) {
		if (loadingInstance != null) {
			loadingInstance.close();
			loadingInstance = null;
		}
		return;
	}
	if (loadingInstance != null) {
		return;
	}
	loadingInstance = Loading.service({
		fullscreen: true,
		lock: true,
		text: options.text || '加载中...',
		spinner: 'el-icon-loading',
		background: options.background || 'rgba(0, 0, 0, 0.7)'
	});
}


/**
 * 时间格式化
 * 年：YYYY
 * 月：MM
 * 日：DD
 * DDDD 星期
 * 时:HH
 * 分:mm
 * 秒：ss
 * 上午/下午：A
 */
export function dateTimeFormat(date, format) {
	if (date && date.toString().length > 0) {
		return moment(date, 'YYYY-MM-DDTHH:mm:ss', 'zh-cn').format(format || "YYYY-MM-DD HH:mm:ss");
	}
	return '';
}

/* 页面跳转 */
export function navigateTo(url) {
	uni.navigateTo({
		url: url
	})
}


/* 手机号码是否脱敏 */
export function mobileFormat(val, isReplacestar) {
	if (!val || val.length === 0) {
		return val;
	}
	let str = val;
	if (isReplacestar && val.length === 11) {
		str = val.toString().replace(/(\d{3})\d*(\d{4})/, '$1****$2');
	}
	return str;
}


export function nameFormat(val, isReplacestar) {
	if (!val || val.length === 0) {
		return val;
	}
	let str = val;
	if (isReplacestar) {
		str = val.toString().replace(/(\S{1})\S*/, '$1**');
	}
	return str;
}

export function idCardFormat(val, isReplacestar) {
	if (!val || val.length === 0) {
		return val;
	}
	let str = val;
	if (isReplacestar && val.length === 18) {
		str = val.toString().replace(/(\d{4})\d*(\d{4})/, '$1**********$2');
	}
	return str;
}


// 定时器
export function interval(key, func, milliscond) {
	let _interval = uni.getStorageSync(key) || undefined;
	if (_interval) {
		clearInterval(_interval);
	}
	_interval = setInterval(() => {
		func()
	}, milliscond)
	uni.setStorageSync(key, _interval);
}


//计算数组所占百分比
export function countPercentage(countArray) {
	var j = countArray.reduce(function(prev, next, index, array) {
		return prev + next;
	})
	var resultArray = [];
	for (var i = 0; i < countArray.length; i++) {
		var k = Math.floor((countArray[i] / j) * 100).toFixed(2) + "%";
		resultArray.push(k);
	}
	return resultArray;
}


export function timeFormat(time) {
	if (time && time.length > 0) {
		let tmp= time.replace('T', ' ').replace(' 00:00:00', '').replace(/年/gm, '-').replace(/月/gm, '-').replace(/日/gm, '');
		let arr=tmp.split('-');
		let str='';
		for (var i = 0; i < arr.length; i++) {
			if(i===0){
				str=arr[i]+"年";
				continue;
			}
			if(i===1){
				str+=arr[i]+"月";
				continue;
			}
			if(i===2){
				str+=arr[i]+"日";
				continue;
			}
		}
		return str;
	}
	return '-';
}
